/*
 * Package il.ac.biu.cs.grossmm.impl.server
 * File BootStrap.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package il.ac.biu.cs.grossmm.impl.server;

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import il.ac.biu.cs.grossmm.api.server.Loader;
import il.ac.biu.cs.grossmm.api.server.Service;
import il.ac.biu.cs.grossmm.api.server.ServiceManager;


/**
 * The server bootstrap service
 *
 */
public class BootStrap implements Service {
	
	final WrapperImpl wrapper;
	final Object rootServiceHint;
	ServiceManager serviceManager;

	public BootStrap(Loader rootLoader, Object rootServiceHint) throws Exception {
		this.rootServiceHint = rootServiceHint;
		this.wrapper = new WrapperImpl(rootLoader);
		
		WrappedImpl wrapped = wrapper.wrap(this);
		
		wrapped.initialize();
		
		this.serviceManager = wrapped.getServiceManager();
	}

	public void start(ServiceManager ignored) throws Exception {
		serviceManager.requireService(rootServiceHint);
	}

	public void stop(Exception exception) {
		serviceManager.releaseService(rootServiceHint);
	}

	public void terminated(Object serviceHint, Exception e) {
		
		
	}
	
	public static void main(String[] args) throws Exception {
		DOMParser p = new DOMParser();
		p.parse("t:\\server.xml");

		Document xml = p.getDocument();

		Element xmlResources = xml.getDocumentElement();
		
		Loader loader = new XmlBasedLoader(xmlResources, new ByClassNameLoader());
		
		BootStrap server = new BootStrap(loader, "main");
		
		server.start(null);
		
		server.stop(null);
	}
}
